package com.wondersgroup.fjzlpt.console.dao.impl.role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuDAO;
@Repository
public class RoleMenuDAOImpl extends BaseJTDAOImpl implements RoleMenuDAO {
	
	@Override
	@Autowired
	@Qualifier("TB_AUTH_ROLE_MENU")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	@Autowired
	@Qualifier("TB_AUTH_MENU")
	private TableBean tbMenu;
	
	/**
	 * 根据角色ID删除角色与菜单的关联关系
	 * @param roleId
	 * @return
	 */
	public void deleteByRoleId(String roleId)
	{
		StringBuilder sbud = new StringBuilder("delete from ").append(this.tb.getTableName()).append(" where roleid=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{roleId});
		this.update(sb);
	}

	/**
	 * 删除角色对应的菜单
	 * @param menuId
	 */
	public void deleteByMenuId(String menuId)
	{
		StringBuilder sbud = new StringBuilder("delete from ").append(this.tb.getTableName()).append(" where MENUID like ?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{"%"+menuId+"%"});
		this.update(sb);
	}

	/**
	 * 查询角色对应菜单
	 * @param roleId
	 * @return
	 */
	public JSONArray queryMenusByRoleId(String roleId)
	{
		StringBuilder sbud = new StringBuilder("select b.MENUID, b.MENUNAME, b.VIEWURL, b.PRE_MENUID, b.MENU_LEVEL, a.NAVIGATE from \n")
				.append(this.tb.getTableName()).append(" a, ").append(this.tbMenu.getTableName()).append(" b where 1=1 \n")
				.append(" and a.MENUID=b.MENUID and a.ROLEID=? \n")
				.append("order by b.MENUID, b.MENU_ORDER");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{roleId});
		return this.queryList(sb);
	}
}
